package com.project.purchase.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.purchase.Service.IGoods;
import com.project.purchase.Service.IPerson;
import com.project.purchase.Service.IProducer;
import com.project.purchase.Service.Iindent;
import com.project.purchase.entity.Goods;
import com.project.purchase.entity.Indent;
import com.project.purchase.entity.Producer;
import com.project.purchase.entity.User;
import com.project.purchase.utils.CommonUtils;
import com.project.purchase.vo.IndentWithPersonProducer;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author VX5
 * @Title: QZP
 * @ProjectName purchase
 * @Description: TODO
 * @date ${DAT}1:19
 */
@Controller
@RequestMapping("/indent")
public class IndentController {
    @Autowired
    Iindent iindent;

    @Autowired
    IPerson iPerson;

    @Autowired
    IGoods iGoods;

    @Autowired
    IProducer iProducer;

    @GetMapping("/get")
    public String GetAllIn(HttpServletRequest request,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }

    /**
     * 填写申请表
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/personadd")
    public String Add(HttpServletRequest request,
                      @RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String goodsname = request.getParameter("goodsname");
        int num = Integer.valueOf(request.getParameter("num"));
        String price = request.getParameter("price");
        int totalprice = Integer.valueOf(request.getParameter("totalprice"));
        int offer = 0;
        String Size = request.getParameter("size");
        String model = request.getParameter("model");
//        String price = request.getParameter("")
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String personID = iPerson.GetIDByLoginName(user.getLoginname());
        Indent indent = new Indent();
        for (int i = 0; i < 3;i++) {
            indent.setIndentid(CommonUtils.getUUID());
            indent.setAdmincheck("待审核");
            indent.setGoodsname(goodsname);
            indent.setTime(" ");
            indent.setNum(num);
            indent.setOffer(offer);
            indent.setPersonid(personID);
            indent.setTotalprice(totalprice);
            indent.setUnivalence(price);
            indent.setSize(Size);
            indent.setModel(model);
            iindent.addPerosn(indent);
        }
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }

    /**
     * 管理员审核
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/adminupdate")
    public String AdminAdd(HttpServletRequest request,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String check = request.getParameter("check");
        String time = request.getParameter("time");
        String id = request.getParameter("indentid");
        String name = request.getParameter("proname");
        String proid = iProducer.GetProIDByName(name);
        Map<String,Object> map = new HashMap<>();
        map.put("admincheck",check);
        map.put("time",time);
        map.put("indentid",id);
        List<IndentWithPersonProducer> list2 = iindent.getByID(id);
        if(check.equals("通过审核")) {
            for (IndentWithPersonProducer indent : list2) {
                Goods goods = new Goods();
                goods.setGoodsid(CommonUtils.getUUID());
                goods.setNum(indent.getNum().toString());
                goods.setSize(indent.getSize());
                goods.setModel(indent.getModel());
                goods.setProducerid(proid);
                goods.setGoodsname(indent.getGoodsname());
                goods.setProducerid(indent.getProducerid());
                goods.setCost(indent.getOffer().toString());
                iGoods.addGoods(goods);
            }
        }
        iindent.UpdateAdminCheck(map);
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }

    /**
     * 用户更改
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/personupdate")
    public String personUpdate(HttpServletRequest request,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        int num = Integer.valueOf(request.getParameter("num"));
        String size = request.getParameter("size");
        String model = request.getParameter("model");
        String price = request.getParameter("price");
        String id = request.getParameter("id");
        int totalprice = Integer.valueOf(request.getParameter("totalprice"));
        Indent indent = new Indent();
        indent.setIndentid(id);
        indent.setNum(num);
        indent.setUnivalence(price);
        indent.setTotalprice(totalprice);
        indent.setModel(model);
        indent.setSize(size);
        iindent.UpdatePerson(indent);
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }

    /**
     * 投标
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/producerupdate")
    public String producerUpdate(HttpServletRequest request,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        int offer = Integer.valueOf(request.getParameter("offer"));
        String name = request.getParameter("name");
        String proid = iProducer.GetProIDByName(name);
        String id = request.getParameter("id");
        Indent indent = new Indent();
        indent.setOffer(offer);
        indent.setProducerid(proid);
        indent.setIndentid(id);
        iindent.UpdatePerson(indent);
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }


    /**
     * 删除订单
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/delet")
    public String deletById(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String id = request.getParameter("id");
        iindent.deletIndentById(id);
        PageHelper.startPage(pageNum,pageSize);
        List list = iindent.GetAllIn();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }

    /**
     * 多条件查询
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/search")
    public String GetAllInByProducerNameModel(HttpServletRequest request,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String goodsname = request.getParameter("goodsname");
        String model = request.getParameter("model");
        Map<String,Object> map = new HashMap<>();
        map.put("goodsname",goodsname);
        map.put("model",model);
        List list = iindent.GetAllInByProducerNameModel(map);
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "indent";
    }
}
